<template>
  <div class="auth-container">
    <!-- 添加浮动气泡 -->
    <div class="bubble"></div>
    <div class="bubble"></div>
    <div class="bubble"></div>
    <div class="bubble"></div>
    <div class="bubble"></div>
    
    <!-- 添加扫光效果 -->
    <div class="light-beam"></div>
    
    <div class="auth-card" :class="{ 'is-register': isRegister }">
      <div class="card-header">
        <div class="switch-tabs">
          <div 
            class="tab-item"
            :class="{ active: !isRegister }"
            @click="isRegister = false"
          >
            登录
          </div>
          <div 
            class="tab-item"
            :class="{ active: isRegister }"
            @click="isRegister = true"
          >
            注册
          </div>
          <div class="active-indicator" :style="indicatorStyle"></div>
        </div>
      </div>

      <div class="card-body">
        <!-- 登录表单 -->
        <form v-if="!isRegister" class="auth-form" @submit.prevent="handleLogin">
          <div class="form-group">
            <div class="input-wrapper">
              <span class="input-icon">📧</span>
              <input
                v-model="loginForm.account"
                type="text"
                placeholder="邮箱/手机号"
                class="form-input"
              >
            </div>
          </div>

          <div class="form-group">
            <div class="input-wrapper">
              <span class="input-icon">🔒</span>
              <input
                v-model="loginForm.password"
                type="password"
                placeholder="请输入密码"
                class="form-input"
              >
            </div>
          </div>

          <div class="form-options">
            <label class="remember-me">
              <input type="checkbox" v-model="loginForm.remember">
              <span>记住我</span>
            </label>
            <a href="#" class="forgot-password">忘记密码？</a>
          </div>

          <button type="submit" class="submit-btn" :disabled="isLoading">
            <span v-if="isLoading" class="loading-icon">⟳</span>
            <span v-else>登录</span>
          </button>

          <div class="divider">
            <span>其他登录方式</span>
          </div>

          <div class="social-login">
            <button type="button" class="social-btn wechat">
              <span class="icon">💬</span>
              微信登录
            </button>
            <button type="button" class="social-btn qq">
              <span class="icon">💫</span>
              QQ登录
            </button>
          </div>
        </form>

        <!-- 注册表单 -->
        <form v-else class="auth-form" @submit.prevent="handleRegister">
          <div class="form-group">
            <div class="input-wrapper">
              <span class="input-icon">📧</span>
              <input
                v-model="registerForm.email"
                type="email"
                placeholder="请输入邮箱"
                class="form-input"
              >
            </div>
          </div>

          <div class="form-group">
            <div class="input-wrapper">
              <span class="input-icon">📱</span>
              <input
                v-model="registerForm.phone"
                type="tel"
                placeholder="请输入手机号"
                class="form-input"
              >
            </div>
          </div>

          <div class="form-group">
            <div class="input-wrapper">
              <span class="input-icon">🔒</span>
              <input
                v-model="registerForm.password"
                type="password"
                placeholder="请设置密码"
                class="form-input"
              >
            </div>
          </div>

          <div class="form-group">
            <div class="input-wrapper">
              <span class="input-icon">🔒</span>
              <input
                v-model="registerForm.confirmPassword"
                type="password"
                placeholder="请确认密码"
                class="form-input"
              >
            </div>
          </div>

          <div class="form-group verification">
            <div class="verification-row">
              <div class="input-wrapper">
                <span class="input-icon">🔑</span>
                <input
                  v-model="registerForm.code"
                  type="text"
                  placeholder="请输入验证码"
                  class="form-input"
                >
              </div>
              <button 
                type="button" 
                class="send-code-btn"
                :disabled="countdown > 0"
                @click="sendVerificationCode"
              >
                {{ countdown > 0 ? `${countdown}s` : '发送验证码' }}
              </button>
            </div>
          </div>

          <button type="submit" class="submit-btn" :disabled="isLoading">
            <span v-if="isLoading" class="loading-icon">⟳</span>
            <span v-else>注册</span>
          </button>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()
const isRegister = ref(false)
const countdown = ref(0)
const isLoading = ref(false)

// 登录表单数据
const loginForm = ref({
  account: '13800000000',
  password: '',
  remember: false
})

// 注册表单数据
const registerForm = ref({
  email: '',
  phone: '',
  password: '',
  confirmPassword: '',
  code: ''
})

// 切换标签指示器样式
const indicatorStyle = computed(() => ({
  transform: isRegister.value ? 'translateX(100%)' : 'translateX(0)'
}))

// 发送验证码
const sendVerificationCode = async () => {
  if (!registerForm.value.phone) {
    ElMessage.warning('请先输入手机号')
    return
  }
  if (!/^1[3-9]\d{9}$/.test(registerForm.value.phone)) {
    ElMessage.warning('请输入正确的手机号')
    return
  }
  
  // TODO: 调用发送验证码接口
  countdown.value = 60
  const timer = setInterval(() => {
    countdown.value--
    if (countdown.value <= 0) {
      clearInterval(timer)
    }
  }, 1000)
}

// 登录处理
const handleLogin = async () => {
  if (!loginForm.value.account || !loginForm.value.password) {
    ElMessage.warning('请填写完整的登录信息')
    return
  }

  try {
    isLoading.value = true

    // 模拟登录成功
    localStorage.setItem('token', 'test-token')
    localStorage.setItem('userInfo', JSON.stringify({
      id: 1,
      username: 'test_user',
      email: loginForm.value.account,
      avatar: '👤'
    }))
    
    // 触发自定义事件，通知Header组件更新登录状态
    try {
      window.dispatchEvent(new Event('login-state-changed'))
    } catch (eventError) {
      console.error('事件分发错误:', eventError)
    }
    
    ElMessage.success('登录成功')
    
    // 使用最安全的方式导航到首页
    setTimeout(() => {
      window.location.href = '/'
    }, 500)
  } catch (error) {
    console.error('登录失败：', error)
    ElMessage.error('登录失败，请重试')
    isLoading.value = false
  }
}

// 注册处理
const handleRegister = async () => {
  if (!registerForm.value.email || !registerForm.value.phone || 
      !registerForm.value.password || !registerForm.value.confirmPassword || 
      !registerForm.value.code) {
    ElMessage.warning('请填写完整的注册信息')
    return
  }

  if (registerForm.value.password !== registerForm.value.confirmPassword) {
    ElMessage.warning('两次输入的密码不一致')
    return
  }

  if (!/^1[3-9]\d{9}$/.test(registerForm.value.phone)) {
    ElMessage.warning('请输入正确的手机号')
    return
  }

  if (!/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(registerForm.value.email)) {
    ElMessage.warning('请输入正确的邮箱地址')
    return
  }

  try {
    isLoading.value = true
    // TODO: 调用注册接口
    ElMessage.success('注册成功')
    isRegister.value = false
  } catch (error) {
    console.error('注册失败：', error)
    ElMessage.error('注册失败，请重试')
  } finally {
    isLoading.value = false
  }
}
</script>

<style scoped lang="scss">
.auth-container {
  min-height: 100vh;
  min-width: $min-width;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: $spacing-huge;
  position: relative;
  overflow: hidden;
  background: linear-gradient(-45deg, #ee7752, #e73c7e, #23a6d5, #23d5ab);
  background-size: 400% 400%;
  animation: gradientBG 15s ease infinite;
  
  // 添加装饰元素
  &::before, &::after {
    content: '';
    position: absolute;
    width: 300px;
    height: 300px;
    border-radius: 50%;
    opacity: 0.2;
    filter: blur(60px);
  }
  
  &::before {
    background: #ff6b6b;
    top: -100px;
    left: -100px;
    animation: float 8s ease-in-out infinite alternate;
  }
  
  &::after {
    background: #36d1dc;
    bottom: -100px;
    right: -100px;
    animation: float 10s ease-in-out infinite alternate-reverse;
  }
  
  // 扫光效果
  .light-beam {
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(
      90deg,
      rgba(255, 255, 255, 0) 0%,
      rgba(255, 255, 255, 0.1) 50%,
      rgba(255, 255, 255, 0) 100%
    );
    transform: skewX(-20deg);
    animation: lightBeam 8s ease-in-out infinite;
    pointer-events: none;
  }
  
  // 添加浮动气泡
  .bubble {
    position: absolute;
    border-radius: 50%;
    opacity: 0.6;
    filter: blur(5px);
    pointer-events: none;
    
    &:nth-child(1) {
      width: 80px;
      height: 80px;
      background: rgba(255, 107, 107, 0.6);
      top: 10%;
      left: 10%;
      animation: bubbleFloat 20s ease-in-out infinite alternate;
    }
    
    &:nth-child(2) {
      width: 60px;
      height: 60px;
      background: rgba(111, 30, 255, 0.4);
      top: 20%;
      right: 20%;
      animation: bubbleFloat 15s ease-in-out infinite alternate-reverse;
    }
    
    &:nth-child(3) {
      width: 120px;
      height: 120px;
      background: rgba(54, 209, 220, 0.5);
      bottom: 15%;
      left: 25%;
      animation: bubbleFloat 25s ease-in-out infinite alternate;
    }
    
    &:nth-child(4) {
      width: 50px;
      height: 50px;
      background: rgba(255, 209, 102, 0.6);
      top: 40%;
      right: 10%;
      animation: bubbleFloat 18s ease-in-out infinite alternate-reverse;
    }
    
    &:nth-child(5) {
      width: 70px;
      height: 70px;
      background: rgba(46, 213, 115, 0.5);
      bottom: 10%;
      right: 15%;
      animation: bubbleFloat 22s ease-in-out infinite alternate;
    }
  }
  
  @media (max-width: 768px) {
    padding: $spacing-base;
  }
}

// 背景渐变动画
@keyframes gradientBG {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

@keyframes float {
  0% {
    transform: translateY(0) rotate(0deg);
  }
  50% {
    transform: translateY(15px) rotate(5deg) scale(1.05);
  }
  100% {
    transform: translateY(30px) rotate(10deg) scale(1);
  }
}

// 气泡浮动动画
@keyframes bubbleFloat {
  0% {
    transform: translate(0, 0) rotate(0deg) scale(1);
  }
  33% {
    transform: translate(30px, -30px) rotate(10deg) scale(1.1);
  }
  66% {
    transform: translate(-20px, 20px) rotate(-5deg) scale(0.9);
  }
  100% {
    transform: translate(10px, -10px) rotate(15deg) scale(1);
  }
}

@keyframes lightBeam {
  0% {
    left: -100%;
    opacity: 0.5;
  }
  10% {
    opacity: 0.8;
  }
  20% {
    opacity: 0.5;
  }
  30%, 100% {
    left: 100%;
    opacity: 0;
  }
}

.auth-card {
  width: 100%;
  max-width: 420px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(15px);
  border-radius: 20px;
  box-shadow: 0 15px 50px rgba(0, 0, 0, 0.2);
  overflow: hidden;
  animation: slideUp 0.6s cubic-bezier(0.16, 1, 0.3, 1);
  position: relative;
  z-index: 10;
  border: 1px solid rgba(255, 255, 255, 0.3);
  transform: perspective(1000px) rotateX(0deg);
  transition: all 0.5s ease;
  
  &:hover {
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
    transform: perspective(1000px) rotateX(2deg);
  }

  &.is-register {
    max-width: 420px;

    .auth-form {
      animation: slideLeft 0.3s cubic-bezier(0.16, 1, 0.3, 1);
    }
  }

  .card-header {
    padding: 0;
    position: relative;
    
    .switch-tabs {
      position: relative;
      display: flex;
      padding: 0;
      background: linear-gradient(to right, #3a7bd5, #00d2ff);

      .tab-item {
        flex: 1;
        padding: $spacing-base;
        text-align: center;
        color: rgba(255, 255, 255, 0.8);
        font-size: 17px;
        font-weight: 500;
        cursor: pointer;
        position: relative;
        z-index: 1;
        transition: all 0.3s ease;
        letter-spacing: 0.5px;
        
        &::before {
          content: '';
          position: absolute;
          bottom: 10px;
          left: 50%;
          transform: translateX(-50%) scaleX(0);
          width: 20px;
          height: 2px;
          background: rgba(255, 255, 255, 0.5);
          opacity: 0;
          transition: all 0.3s ease;
        }

        &.active {
          color: white;
          
          &::before {
            opacity: 1;
            transform: translateX(-50%) scaleX(1);
          }
        }
        
        &:hover:not(.active) {
          color: white;
          
          &::before {
            opacity: 0.5;
            transform: translateX(-50%) scaleX(0.5);
          }
        }
      }

      .active-indicator {
        position: absolute;
        bottom: 0;
        left: 0;
        height: 100%;
        width: 50%;
        background: rgba(255, 255, 255, 0.15);
        transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
        z-index: 0;
        border-radius: 0;
        box-shadow: 0 0 20px rgba(255, 255, 255, 0.2) inset;
        backdrop-filter: blur(5px);
        border-top: 2px solid rgba(255, 255, 255, 0.6);
      }
    }
  }

  .card-body {
    padding: $spacing-large $spacing-huge;
    background-color: white;
  }
}

.auth-form {
  animation: slideRight 0.3s ease;

  .form-group {
    margin-bottom: $spacing-large;

    .input-wrapper {
      position: relative;
      background: white;
      border-radius: $border-radius-base;
      box-shadow: 0 3px 8px rgba(0, 0, 0, 0.05);
      transition: all 0.3s ease;
      overflow: hidden;
      
      &:focus-within {
        box-shadow: 0 3px 12px rgba(58, 123, 213, 0.15);
        border-color: var(--el-color-primary);
      }

      &::before {
        content: '';
        position: absolute;
        left: 0;
        top: 0;
        height: 100%;
        width: 3px;
        background: linear-gradient(to bottom, #3a7bd5, #00d2ff);
        opacity: 0;
        transition: opacity 0.3s ease;
      }

      &:focus-within::before {
        opacity: 1;
      }

      .input-icon {
        position: absolute;
        left: $spacing-base;
        top: 50%;
        transform: translateY(-50%);
        font-size: 18px;
        color: $text-secondary;
        transition: all 0.3s ease;
      }

      .form-input {
        width: 100%;
        height: 48px;
        padding: 0 $spacing-base 0 44px;
        border: 1px solid $border-light;
        border-radius: $border-radius-base;
        font-size: 15px;
        transition: all 0.3s ease;
        background: transparent;

        &::placeholder {
          color: $text-placeholder;
          transition: all 0.3s ease;
        }

        &:focus {
          border-color: var(--el-color-primary);
          box-shadow: none;
          outline: none;
          
          &::placeholder {
            opacity: 0.7;
            transform: translateX(3px);
          }
          
          & + .input-icon {
            color: var(--el-color-primary);
          }
        }
      }
    }

    &.verification {
      .verification-row {
        display: flex;
        gap: $spacing-base;

        .input-wrapper {
          flex: 1;
        }

        .send-code-btn {
          width: 120px;
          border: none;
          background: linear-gradient(to right, #3a7bd5, #00d2ff);
          color: white;
          border-radius: $border-radius-base;
          cursor: pointer;
          transition: all 0.3s ease;
          font-weight: 500;
          font-size: 14px;
          box-shadow: 0 3px 8px rgba(58, 123, 213, 0.2);

          &:hover {
            transform: translateY(-1px);
            box-shadow: 0 5px 12px rgba(58, 123, 213, 0.3);
          }

          &:disabled {
            background: $border-color;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
          }
        }
      }
    }
  }

  .form-options {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: $spacing-large;

    .remember-me {
      display: flex;
      align-items: center;
      gap: $spacing-mini;
      color: $text-regular;
      cursor: pointer;
      font-size: 14px;

      input[type="checkbox"] {
        width: 16px;
        height: 16px;
        cursor: pointer;
        accent-color: var(--el-color-primary);
      }
    }

    .forgot-password {
      color: var(--el-color-primary);
      text-decoration: none;
      font-size: 14px;
      position: relative;
      
      &::after {
        content: '';
        position: absolute;
        bottom: -2px;
        left: 0;
        width: 100%;
        height: 1px;
        background-color: var(--el-color-primary);
        opacity: 0;
        transform: scaleX(0.7);
        transition: all 0.3s ease;
      }

      &:hover {
        text-decoration: none;
        
        &::after {
          opacity: 1;
          transform: scaleX(1);
        }
      }
    }
  }

  .submit-btn {
    width: 100%;
    height: 48px;
    border: none;
    border-radius: $border-radius-base;
    background: linear-gradient(to right, #3a7bd5, #00d2ff);
    color: white;
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;
    margin-top: $spacing-large;
    box-shadow: 0 4px 15px rgba(64, 158, 255, 0.2);

    .loading-icon {
      display: inline-block;
      animation: spin 1s linear infinite;
      font-size: 18px;
    }

    &:disabled {
      opacity: 0.7;
      cursor: not-allowed;
      transform: none;
      box-shadow: none;
    }

    &:hover:not(:disabled) {
      transform: translateY(-2px);
      box-shadow: 0 6px 20px rgba(64, 158, 255, 0.3);
    }
    
    // 按钮扫光效果
    &::after {
      content: '';
      position: absolute;
      top: 0;
      left: -100%;
      width: 100%;
      height: 100%;
      background: linear-gradient(
        90deg,
        rgba(255, 255, 255, 0) 0%,
        rgba(255, 255, 255, 0.2) 50%,
        rgba(255, 255, 255, 0) 100%
      );
      transform: skewX(-20deg);
      animation: buttonShine 4s ease-in-out infinite;
    }
  }

  .divider {
    position: relative;
    text-align: center;
    margin: $spacing-large 0;

    &::before,
    &::after {
      content: '';
      position: absolute;
      top: 50%;
      width: calc(50% - 80px);
      height: 1px;
      background: $border-color;
      transition: all 0.3s ease;
    }

    &::before {
      left: 0;
    }

    &::after {
      right: 0;
    }

    span {
      background: white;
      padding: 0 $spacing-base;
      color: $text-secondary;
      font-size: 14px;
      position: relative;
      z-index: 1;
    }

    &:hover {
      &::before, &::after {
        background: $border-light;
      }
    }
  }

  .social-login {
    display: flex;
    gap: $spacing-base;

    .social-btn {
      flex: 1;
      height: 48px;
      border: 1px solid $border-light;
      border-radius: $border-radius-base;
      background: white;
      color: $text-regular;
      font-size: 14px;
      cursor: pointer;
      transition: all 0.3s ease;
      display: flex;
      align-items: center;
      justify-content: center;
      gap: $spacing-mini;
      box-shadow: 0 2px 6px rgba(0, 0, 0, 0.03);

      .icon {
        font-size: 18px;
        transition: all 0.3s ease;
      }

      &:hover {
        transform: translateY(-1px);
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
      }

      &.wechat:hover {
        border-color: #07c160;
        color: #07c160;
        
        .icon {
          transform: scale(1.1);
        }
      }

      &.qq:hover {
        border-color: #12b7f5;
        color: #12b7f5;
        
        .icon {
          transform: scale(1.1);
        }
      }
    }
  }
}

@keyframes buttonShine {
  0% {
    left: -100%;
  }
  20%, 100% {
    left: 100%;
  }
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px) perspective(1000px) rotateX(10deg);
  }
  to {
    opacity: 1;
    transform: translateY(0) perspective(1000px) rotateX(0deg);
  }
}

@keyframes slideLeft {
  from {
    opacity: 0;
    transform: translateX(30px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes slideRight {
  from {
    opacity: 0;
    transform: translateX(-30px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
</style> 